<?php
header('Content-Type: text/html; charset=UTF-8');

$mudi = trim(''. @$_GET['mudi']);
if ($mudi == 'autoRun'){
	require(dirname(__FILE__) .'/configVer.php');
}else{
	require(dirname(__FILE__) .'/check.php');
}



switch ($mudi){
	case 'autoRun':
		AutoRun();
		break;

	case 'autoRunBig':
		AutoRunBig();
		break;

	case 'data':
		DataDeal();
		break;

	default:
		die('err');
}



// 自动操作
function AutoRun(){
	$mode = trim(''. @$_GET['mode']);
	$sec = trim(''. @$_GET['sec']);
		if (is_numeric($sec) == false){ $sec = 300; }
		if ($sec < 60){ $sec = 300; }
	?>

	<!DOCTYPE html>
	<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
		<title>自动操作检测</title>
		<script language="javascript" type="text/javascript" src="js/inc/jquery.min.js?v=<?php echo(OT_VERSION); ?>"></script>
		<script language="javascript" type="text/javascript" src="cache/js/autoRunSys.js?v=<?php echo(date('dHis')); ?>"></script>
	</head>
	<body>
		<div id="countSec" style="color:red"></div>

		<script language='javascript' type='text/javascript'>
		var dbPathPart='';
		var pcPathPart="";
		var webPathPart='';
		var jsPathPart='';
		var webTypeName="home";

		var runSec = <?php echo($sec); ?>;	// 秒数
		function CountDown(){
			if (runSec >= 0){
				document.getElementById("countSec").innerHTML = "还有 "+ runSec +" 秒刷新本页面";
				runSec --;
			}else{
				clearInterval(timer);
				window.location.reload();
			}
		}
		timer = setInterval("CountDown()", 1000);

		// GET提交AJAX处理
		function AjaxGetDeal(urlStr){
			$.ajaxSetup({cache:false});
			$.get(urlStr, function(result){
				eval(result.replace(/<(script[^>]*?)>/gi,"").replace(/<\/script.*?>/gi,"").replace(/(<meta[^>]*>|<\/meta>)/gi,""));
			});
			return false;
		}

		var myDate = new Date();
		var timestamp = Date.parse(myDate);
		var mode = "<?php echo($mode); ?>";
		timestamp = timestamp/1000;
		var isRun = 0;
		if (mode == "wap"){
			document.title = "WAP自动操作检测";
			if (ARS_isHtmlHome == 1 && ARS_htmlHomeWapTimer + ARS_htmlHomeMin * 60 < timestamp){
				isRun = 1;
			}
			if (isRun == 0 && ARS_isHtmlList == 1 && ARS_htmlListWapTimer + ARS_htmlListMin * 60 < timestamp){
				isRun = 1;
			}
			if (isRun == 0 && ARS_isHtmlShow == 1 && ARS_htmlShowWapTimer + ARS_htmlShowMin * 60 < timestamp){
				isRun = 1;
			}
		}else{
			document.title = "PC自动操作检测";
			if (ARS_isHtmlHome == 1 && ARS_htmlHomeTimer + ARS_htmlHomeMin * 60 < timestamp){
				isRun = 1;
			}
			if (isRun == 0 && ARS_isHtmlList == 1 && ARS_htmlListTimer + ARS_htmlListMin * 60 < timestamp){
				isRun = 1;
			}
			if (isRun == 0 && ARS_isHtmlShow == 1 && ARS_htmlShowTimer + ARS_htmlShowMin * 60 < timestamp){
				isRun = 1;
			}
		}
		if (isRun == 0 && ARS_isColl == 1 && ARS_collTimer + ARS_collMin * 60 < timestamp){
			isRun = 1;
		}
		if (isRun == 0 && ARS_isApiItem == 1 && ARS_apiItemTimer + ARS_apiItemMin * 60 < timestamp){
			isRun = 1;
		}
		if (isRun == 0 && ARS_isTimeRun == 1 && ARS_timeRunTimer + ARS_timeRunMin * 60 < timestamp){
			isRun = 1;
		}
		if (isRun == 0 && ARS_isDayRun == 1 && ARS_dayRunTimer < (Date.parse(myDate.getDate() +'/'+ myDate.getMonth() +'/'+ myDate.getFullYear())/1000)){
			isRun = 1;
		}
		// if (isRun == 1){ // #topUserBox
			ARS_runMode=0;
			$("body").append(''+
				'<iframe id="autoRun_time" name="autoRun_time" src="about:blank" width="250" height="200"></iframe>'+
				'<iframe id="autoRun_day" name="autoRun_day" src="about:blank" width="250" height="200"></iframe>'+
				'<iframe id="autoRun_home" name="autoRun_home" src="about:blank" width="250" height="200"></iframe>'+
				'<iframe id="autoRun_list" name="autoRun_list" src="about:blank" width="250" height="200"></iframe>'+
				'<iframe id="autoRun_show" name="autoRun_show" src="about:blank" width="250" height="200"></iframe>'+
				'<iframe id="autoRun_coll" name="autoRun_coll" src="about:blank" width="850" height="500"></iframe>'+
				'<iframe id="autoRun_apiItem" name="autoRun_apiItem" src="about:blank" width="850" height="500"></iframe>'+
				'');
			var arHome_window=window.frames["autoRun_home"];
			arHome_window.window.alert=function(){ return false; };
			var arList_window=window.frames["autoRun_list"];
			arList_window.window.alert=function(){ return false; };
			var arShow_window=window.frames["autoRun_show"];
			arShow_window.window.alert=function(){ return false; };
			var arColl_window=window.frames["autoRun_coll"];
			arColl_window.window.alert=function(){ return false; };
			var arApiItem_window=window.frames["autoRun_apiItem"];
			arApiItem_window.window.alert=function(){ return false; };

			if (mode == "wap"){
				AjaxGetDeal("wap/p.php?m=autoRun&type=duli&isAjaxRun="+ ARS_runMode +"&rnd="+ timestamp);
			}else{
				AjaxGetDeal("p.php?m=autoRun&type=duli&isAjaxRun="+ ARS_runMode +"&rnd="+ timestamp);
			}
		/* }else{
			document.write("暂无可执行的自动操作。");
		} */
		</script>
	</body>
	</html>

	<?php
}


// 自动操作-大数据量项目
function AutoRunBig(){
	$sec	= OT::GetInt('sec');
	$type	= OT::GetStr('type');
		if ($sec < 600){ $sec = 3600; }
	?>

	<!DOCTYPE html>
	<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
		<title>大数据量项目检测</title>
		<script language="javascript" type="text/javascript" src="js/inc/jquery.min.js?v=<?php echo(OT_VERSION); ?>"></script>
	</head>
	<body>
		<div id="countSec" style="color:red"></div>

		<script language='javascript' type='text/javascript'>
		var setSec = <?php echo($sec); ?>;	// 开始秒数
		var runSec = <?php echo($sec); ?>;	// 倒计时秒数
		function ResetSec(){
			runSec = setSec;
		}

		// 获取元素id
		function $id(str){
			return document.getElementById(str);
		}

		function CountDown(){
			if (runSec >= 0){
				document.getElementById("countSec").innerHTML = "还有 "+ runSec +" 秒刷新框架页面（每"+ Math.round(setSec/60) +"分钟刷新一次）";
				runSec --;
			}else{
				// clearInterval(timer);
				// window.location.reload();
				ResetSec();
				var arHome_window=window.frames["autoRun_home"];
				arHome_window.window.location.reload();
			}
		}
		timer = setInterval("CountDown()", 1000);
		</script>

		<div style="font-weight:bold;padding:16px 0;">【运行日志】</div>
		<div id="logBox"></div>
		<iframe id="autoRun_bigItem" name="autoRun_bigItem" src="?mudi=data&type=<?php echo($type); ?>" width="600" height="200"></iframe>
	</body>
	</html>

	<?php
}



// 数据执行
function DataDeal(){
	global $DB, $autoRunSysArr;

	$point	= OT::GetInt('point');
	$type	= OT::GetStr('type');

	$itemArr = array();
	$getArr = BatchRun::BigItemArr();
	foreach ($getArr as $key => $val){
		$itemArr[] = array('type' => $key, 'theme' => $val);
	}

	if (strlen($type) > 0){
		$itemArr = array();
		$getArr = BatchRun::DefItemArr();
		if (isset($getArr[$type])){
			$itemArr = array();
			$itemArr[] = array('type' => $type, 'theme' => $getArr[$type]);
			JS::Diy('parent.document.title = "批处理：'. $getArr[$type] .'";');
			// LogJs('【'. $getArr[$type] .'】只运行该项目...');
		}else{
			die('没找到符合“'. Str::RegExp($type, 'sql') .'”的信息。');
		}
	}else{
		if (empty($autoRunSysArr)){ $autoRunSysArr = Cache::PhpFile('autoRunSys'); }
		if (strtotime($autoRunSysArr['ARS_bigItemTime']) + $autoRunSysArr['ARS_bigItemDay']*86400 >= time()){	// 86400秒=1天
			LogJs('未到时间 '. $autoRunSysArr['ARS_bigItemTime'] .'['. $autoRunSysArr['ARS_bigItemDay'] .']', 'margin-top:16px;');
		}
	}

	$todayTime = TimeDate::Get();

	$itemCount = count($itemArr);
	if ($point < 0){ $point = 0; }
	if ($point >= $itemCount){
		if (strlen($type) == 0){
			$DB->query('update '. OT_dbPref .'autoRunSys set ARS_bigItemTime='. $DB->ForTime($todayTime));
		}
		LogJs('（'. $point .'/'. $itemCount .'）已运行完，等待下次运行', 'margin-top:16px;');
		die();
	}

	$currArr = $itemArr[$point];

	LogJs('【'. $currArr['theme'] .'】正在检查中...', 'margin-top:16px;');

	switch ($currArr['type']){
		case 'prevNext0':	// 上下篇文章未赋值处理
			$retArr = BatchRun::PrevNext('empty');
			break;
	
		case 'prevNext':	// 上下篇文章ID更新
			$retArr = BatchRun::PrevNext();
			break;
	
		case 'markNews0':	// 相关文章未赋值处理
			$retArr = BatchRun::MarkNews('empty');
			break;
	
		case 'markNews':	// 相关文章ID更新
			$retArr = BatchRun::MarkNews();
			break;
	
		case 'autoImg':		// 缩略图为空自动配图
			$retArr = BatchRun::AutoImg();
			break;
	
		default :
			LogJs('暂无其他项目运行');
			die();
			break;
	}	// $retArr = array('res'=>true/false是否有成功执行项目, 'isEnd'=>true/false项目是否运行完成, 'note'=>'备注信息');

	if ($retArr['res']){
		$logArr = array();
		$logArr['ARL_time']		= $todayTime;
		$logArr['ARL_type']		= 'bigItem';
		$logArr['ARL_dataID']	= 0;
		$logArr['ARL_content']	= '【'. $currArr['theme'] .'】'. $retArr['note'] .'';
		$DB->InsertParam('autoRunLog',$logArr);
	}
	LogJs('【'. $currArr['theme'] .'】'. $retArr['note'] .'');
	if ($retArr['isEnd']){
		$point ++;
		JS::HrefTimeoutEnd('?mudi=data&point='. $point .'&type='. $type, 3);
	}else{
		JS::HrefTimeoutEnd('?mudi=data&point='. $point .'&type='. $type, 2);
	}
}


function LogJs($str, $style=''){
	echo('
	<script language="javascript" type="text/javascript">
	parent.$id("logBox").innerHTML += "<div style=\"'. $style .'\">【'. TimeDate::Get() .'】'. $str .'</div>";
	</script>
	<div>正在运行：'. $str .'</div>
	');
}

?>